/*
 * Copyright (c)e 2024-2024.Powered by neflibata
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.liuzian.marketserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzian.marketserver.dto.R;
import com.liuzian.marketserver.entity.Address;
import com.liuzian.marketserver.mapper.AddressMapper;
import com.liuzian.marketserver.service.AddressService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Override
    public R<String> addAddress(Address address) {
        boolean bool = super.save(address);
        if (bool) {
            return R.success("地址添加成功");
        } else {
            return R.error("地址添加失败");
        }
    }

    @Override
    public R<String> setDefaultAddress(Long aid, Long uid) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUid, uid);
        queryWrapper.eq(Address::getIsDefault, 1);
        Address address = super.getOne(queryWrapper);
        LambdaUpdateWrapper<Address> updateWrapper;
        if (address == null) {
            updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Address::getAid, aid);
            updateWrapper.set(Address::getIsDefault, 1);
            boolean b = super.update(updateWrapper);
            if (b) {
                return R.success("成功设置默认地址");
            } else {
                return R.error("设置默认地址失败");
            }
        }
        updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Address::getUid, uid);
        updateWrapper.eq(Address::getIsDefault, 1);
        updateWrapper.set(Address::getIsDefault, 0);
        boolean bool = super.update(updateWrapper);
        if (bool) {
            updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Address::getAid, aid);
            updateWrapper.set(Address::getIsDefault, 1);
            boolean b = super.update(updateWrapper);
            if (b) {
                return R.success("成功设置默认地址");
            } else {
                return R.error("设置默认地址失败");
            }
        } else {
            return R.error("设置默认地址失败");
        }
    }

    @Override
    public R<List<Address>> getAddressByUid(Long uid) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUid, uid);
        queryWrapper.eq(Address::getIsDefault, 0);
        List<Address> addressList = super.list(queryWrapper);
        return R.success(addressList);
    }


    @Override
    public R<Address> getDefaultAddressByUid(Long uid) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUid, uid);
        queryWrapper.eq(Address::getIsDefault, 1);
        Address address = super.getOne(queryWrapper);
        return R.success(address);
    }
}
